package com.sunky.rfidapp.util

import android.app.AlertDialog
import android.app.DatePickerDialog
import android.content.Context
import android.content.DialogInterface

fun Context.showSingleChoiceDialog(selected: String, list: Array<String>,
                                   listener: DialogInterface.OnClickListener) {
    var selectedIndex = 0
    list.indices.forEach {
        if (list[it] == selected) {
            selectedIndex = it
            return@forEach
        }
    }
    AlertDialog.Builder(this)
            .setSingleChoiceItems(list, selectedIndex) { _, i ->
                selectedIndex = i
            }
            .setNegativeButton("取消",null)
            .setPositiveButton("确定") { d, _ -> listener.onClick(d,selectedIndex) }
            .create()
            .show()
}

fun Context.showDatePickerDialog(year : Int, month : Int,day : Int, listener: DatePickerDialog.OnDateSetListener) {
    var dialog = DatePickerDialog(this, listener, year,month, day)
    dialog.show()
}

fun Context.showInfoDialog(info : String){
    if (getSharedPreferences("config",0).getBoolean("is_debug_on",false)) {
        AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage(info)
                .setPositiveButton("确定", null)
                .create()
                .show()
    }
}